package com.oa.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.oa.bean.student.Rewardsp;
import com.oa.bean.student.Says;
import com.oa.bean.student.Student;
import com.oa.dao.StudentDAO;

public class StudentDAOImpl implements StudentDAO {
	
	private SessionFactory sessionFactory;
	
	private Session session;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public Says saysAdd(Says say) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(say);
		session.getTransaction().commit();
		return say;
	}

	@Override
	public Says saysUpdate(Says say) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.update(say);
		session.getTransaction().commit();
		return say;
	}

	@Override
	public void saysDelete(Says say) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.delete(say);
		session.getTransaction().commit();
	}

	@Override
	public List<Says> saysQueryPagerBySayscon(int page, int pageSize, Says say) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Says where sayface=? or eid=? or sayscon like '%"+say.getSayscon()+"%'");
		query.setInteger(0, say.getStudents().getIntenid());
		query.setInteger(1, say.getEmp().getEid());
		query.setFirstResult((page - 1) * pageSize);// 设置从哪里开始查询，0开始第一条
		query.setMaxResults(pageSize);// 设置查询的total(数量)
		@SuppressWarnings("unchecked")
		List<Says> list = query.list();
		return list;
	}

	@Override
	public Rewardsp rewardspAdd(Rewardsp rewardsp) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(rewardsp);
		session.getTransaction().commit();
		session.close();
		return rewardsp;
	}

	@Override
	public Rewardsp rewardspUpdate(Rewardsp rewardsp) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.update(rewardsp);
		session.getTransaction().commit();
		return rewardsp;
	}

	@Override
	public void rewardspDelete(Rewardsp rewardsp) {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.delete(rewardsp);
		session.getTransaction().commit();
	}

	@Override
	public List<Rewardsp> rewardspQueryPagerByObject(int page, int pageSize, Rewardsp rewardsp) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Rewardsp where jstuid=? ");
		query.setInteger(0,  rewardsp.getStudents().getIntenid());
		query.setFirstResult((page - 1) * pageSize);// 设置从哪里开始查询，0开始第一条
		query.setMaxResults(pageSize);// 设置查询的total(数量)
		@SuppressWarnings("unchecked")
		List<Rewardsp> list = query.list();
		return list;
	}

	@Override
	public List<Rewardsp> rewardspQueryPagerByDate(int page, int pageSize, Date date) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Student stuSave(Student stu) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.save(stu);
		tr.commit();
		return stu;
	}

	@Override
	public Student stuUpdate(Student stu) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		session.saveOrUpdate(stu);
		tr.commit();
		return stu;
	}

	@Override
	public void stuDelete(int id) {
		Session session = sessionFactory.getCurrentSession();
		Transaction tr = session.beginTransaction();
		Student stu = new Student();
		stu.setIntenid(id);
		session.delete(stu);
		tr.commit();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Student> StuQueryByPage(int pageNo, int pageSize) {
		session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Student.class);
		criteria.setFirstResult((pageNo-1)*pageSize);
		criteria.setMaxResults(pageSize);
		List<Student> stu = criteria.list();
		return stu;
	}

	@Override
	public void getClose() {
		session.close();
	}

}
